package com.jlnk.retrofitdemo.views;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.jlnk.retrofitdemo.R;
import com.jlnk.retrofitdemo.adapter.HotSearchAdapter;
import com.jlnk.retrofitdemo.response.HotNewsResponse;
import com.jlnk.retrofitdemo.response.TranslationResponse;
import com.jlnk.retrofitdemo.service.RetrofitClient;
import com.jlnk.retrofitdemo.service.TenApi;
import com.jlnk.retrofitdemo.service.TransApi;

import java.util.ArrayList;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private EditText editText;
    private TenApi tenApi;
    private TransApi transApi;
    private HotSearchAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        recyclerView = findViewById(R.id.recycler_view);
        editText = findViewById(R.id.edit);
        Button buttonGet = findViewById(R.id.button_get);
        Button buttonPost = findViewById(R.id.button_post);

        Retrofit tenRetrofit = RetrofitClient.getTenClient();
        Retrofit transRetrofit = RetrofitClient.getYuanXiClient();

        tenApi = tenRetrofit.create(TenApi.class);
        transApi = transRetrofit.create(TransApi.class);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new HotSearchAdapter(this, new ArrayList<>());
        recyclerView.setAdapter(adapter);

        buttonGet.setOnClickListener(v -> getHotSearch());
        buttonPost.setOnClickListener(v -> postTranslate());
    }

    private void getHotSearch() {
        Call<HotNewsResponse> call = tenApi.getHotNews();
        call.enqueue(new Callback<HotNewsResponse>() {
            @Override
            public void onResponse(Call<HotNewsResponse> call, Response<HotNewsResponse> response) {
                if (response.isSuccessful() && response.body() != null) {
                    runOnUiThread(() -> displayHotSearchData(response.body()));
                } else {
                    runOnUiThread(() -> showError("GET Error: HTTP " + response.code() + " " + response.message()));
                }
            }

            @Override
            public void onFailure(Call<HotNewsResponse> call, Throwable t) {
                runOnUiThread(() -> showError("Network Error: " + t.getMessage()));
            }
        });
    }

    private void displayHotSearchData(HotNewsResponse response) {
        if (response.getResults() != null && !response.getResults().isEmpty()) {
            adapter.updateData(response.getResults());
            adapter.notifyDataSetChanged();
        } else {
            Toast.makeText(this, "No data to display", Toast.LENGTH_SHORT).show();
        }
    }

    private void postTranslate() {
        String text = editText.getText().toString();
        if (!text.isEmpty()) {
            Call<TranslationResponse> call = transApi.postTranslation(text);
            call.enqueue(new Callback<TranslationResponse>() {
                @Override
                public void onResponse(Call<TranslationResponse> call, Response<TranslationResponse> response) {
                    if (response.isSuccessful() && response.body() != null) {
                        runOnUiThread(() -> displayTranslationData(response.body()));
                    } else {
                        runOnUiThread(() -> showError("POST Error: HTTP " + response.code() + " " + response.message()));
                    }
                }

                @Override
                public void onFailure(Call<TranslationResponse> call, Throwable t) {
                    runOnUiThread(() -> showError("Network Error: " + t.getMessage()));
                }
            });
        } else {
            Toast.makeText(this, "Please enter text to translate", Toast.LENGTH_SHORT).show();
        }
    }

    private void displayTranslationData(TranslationResponse response) {
        if (response != null) {
            String resultText = "原文: " + response.getContent() + "\n" +
                    "翻译: " + response.getResult();
            Toast.makeText(MainActivity.this, resultText, Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(MainActivity.this, "Translation result is null", Toast.LENGTH_SHORT).show();
        }
    }

    private void showError(String message) {
        Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
    }
}